import { request } from '@/base/Request';
import { Device } from '@/base/model/Device';
import { DevVo } from '@/base/model/DevVo';
import { ExcelError } from '@/base/model/ExcelError';
import { DeviceDistibuteVo } from '@/base/model/DeviceDistibuteVo';
import qs from 'qs';
import { Result } from '@/base/Result';
import { DeviceMemberVo } from '@/base/model/DeviceMemberVo';
import { DeviceStatusVo } from '@/base/model/DeviceStatusVo';

export function postDevice(params: {
  deviceSn: string,
  modelId: number,
  sortId: number,
}) {
  return request.post(Device, '/api/device', qs.stringify(params));
}

export function getDevice(params: {
  pageIndex: number,
  pageSize: number,
  sortDirection: string,
  agentId: [],
  cityId: string,
  companyId: number,
  createBegin: number,
  createEnd: number,
  deviceSn: string,
  modelId: number,
  modelName: string,
  provinceId: string,
  sortId: number,
  statusCode: string
}) {
  // params.agentId = qs.stringify(params.agentId, {arrayFormat: 'repeat'})
  // qs.parse(params.agentId)
  // console.log(params)
  let agentString = '';
  if (params.agentId !== undefined && params.agentId.length > 0) {
    const agents = params.agentId.map((x: string) => {
      return 'agentId=' + x;
    });
    agentString = agents.join('&');
  }
  delete (params.agentId);
  return request.getPage(DevVo, '/api/device?' + agentString, { params });
}

export function getDeviceId(
  id: number
) {
  return request.get(Device, `/api/device/${id}`);
}

export function PutDeviceId(params: {
  sortId: number,
  modelId: number,
  startImage: string,
  id: number,
  deviceSn: string
}) {
  return request.put(Device, `/api/device/${params.id}`, qs.stringify(params));
}

export function deteDeviceId(
  id: number
) {
  return request.delete(`/api/device/${id}`);
}

export function PutDeviceBatchAgent(params: {
  agentId: number,
  deviceSn: object
}) {
  return request.put(Device, `/api/device/batch/agent`, qs.stringify(params, { arrayFormat: 'repeat' }));
}

export function PutDeviceBatchDelete(params: {
  deviceSn: object
}) {
  return request.put(Device, `/api/device/batch/delete`, qs.stringify(params, { arrayFormat: 'repeat' }));
}

export function PutDeviceBatchSort(params: {
  deviceSn: number[],
  modelId: number,
  sortId: number,
}) {
  return request.put(Device, `/api/device/batch/sort`, qs.stringify(params, { arrayFormat: 'repeat' }));
}

export function getErrorExcel(params: {
  url: string
}) {
  return request.get(ExcelError, `/api/device/error/excel`, { params });
}

export function getDeviceBatchExcel(params: {
  dispose: string,
  url: string
}) {
  return request.get(String, `/api/device/batch/excel`, { params });
}

export function getDevcieDistribute(params: {
  pageIndex: number,
  pageSize: number,
  sortDirection: string,
  companyId?: number[],
  createBegin?: string,
  createEnd?: string,
  agentId?: number[]
}) {
  let companyIdString = '';
  let agentString = '';
  if (params.companyId && params.companyId.length > 0) {
    companyIdString = params.companyId.map(x => 'companyId=' + x).join('&');
    delete params.companyId;
  }
  if (params.agentId && params.agentId.length > 0) {
    agentString = params.agentId.map(x => 'agentId=' + x).join('&');
    delete params.agentId;
  }
  return request.getPage(DeviceDistibuteVo, '/api/device/distribute?' + companyIdString + '&' + agentString, { params });
}

// 获取品牌商绑定的用户数量
export function getDevNum(params: {
  companyId: number
}): Promise<Result<Number>> {
  return request.get(Number, '/api/device/dev-num', { params });
}

export function getDeviceMember(params: {
  id: number
}): Promise<Result<Array<DeviceMemberVo>>> {
  return request.getArray(DeviceMemberVo, '/api/device/member', { params });
}

export function getDeviceStatusNum(params: {
  deviceModelId?: number
}): Promise<Result<Array<DeviceStatusVo>>> {
  return request.getArray(DeviceStatusVo, '/api/device/status-num', { params });
}



